﻿using System;
using System.Collections.Generic;
using CSharpConsoleTestSolution.Bean;

namespace CSharpConsoleTestSolution.Demo
{
    /**
     * https://www.bilibili.com/video/BV1wx411K7rb?p=10
     * P10 操作符详解_1, 时间05:00
     *
     * 类别       运算符
     * 基本   x.y  f(x)  a[x] x++ x-- new typeof default checked unchecked delegate sizeof ->
     * 一元   +  -  !  ~  ++x  --x  (T)x  await  &x  *x
     * 乘法   *  /  %
     * 加减   +  -
     * 移位   <<  >>
     * 关系和类型检测  <  >  <=  >=  is  as
     * 相等           ==  !=
     * 逻辑"与”       &
     * 逻辑 XOR       ^
     * 逻辑 OR        |
     * 条件 AND       &&
     * 条件 OR        ||
     * null 合并      ??
     * 条件                  ?:
     * 赋值和 lambda 表达式   =  *=  /=  %=  +=  -=  <<=  >>=  &=  ^=  |=  =>
     * 运算符
     */
    public class P10_Operator1 {
        /**
         * operator 操作符: 时间13:45
         */
        public static void operator_exam() {
            Person p1 = new Person("Deer");
            Person p2 = new Person("Wife");
            var nation = Person.GetMarry(p1, p2);
            foreach (var person in nation) {
                Console.WriteLine(person.Name);
            }

            Console.WriteLine("\n------------------------\n");

            //operator +
            var nation2 = p1 + p2;
            foreach (var person in nation2) {
                Console.WriteLine(person.Name);
            }
        }

        /**
         * delegate 方法委托: 时间35:10
         */
        public static void delegate_Action_exam() {
            /**
             * Action() 括号内参数: (static) void Xxx()
             *                     (static): 可以是静态方法, 也可以不是静态方法
             *                     void    : 返回值类型必须是void
             *                     Xxx()   : 方法必须是空参
             */
            var action = new Action(Person.PrintHelloStatic);
            var person = new Person("张三");
            var action2 = new Action(person.PrintHello);
            action.Invoke();
            action(); //调用方法
            action2.Invoke();
            action2();
        }

        /**
         * Dictionary 字典类: 时间42:10
         */
        public static void Dictionary_exam() {
            IDictionary<string, Person> stuDic = new Dictionary<string, Person>();
            for (var i = 0; i < 100; i++) {
                var person = new Person("张三_" + i) {
                    Score = i
                };
                stuDic.Add(person.Name, person);
            }

            //使用字典
            var student = stuDic["张三_6"];
            Console.WriteLine(student.Score);
        }

        /**
         * typeof() 操作符: 时间49:30
         */
        public static void typeof_exam() {
            var typeInt = typeof(int);
            Console.WriteLine(typeInt.Namespace); //System
            Console.WriteLine(typeInt.FullName); //System.Int32
            Console.WriteLine(typeInt); //System.Int32
            Console.WriteLine(typeInt.Name); //Int32

            var methods = typeInt.GetMethods();
            foreach (var methodInfo in methods) {
                /*
                 * CompareTo
                 * CompareTo
                 * Equals
                 * Equals
                 * GetHashCode
                 * ToString
                 * ToString
                 * ToString
                 * ToString
                 * Parse
                 * Parse
                 * Parse
                 * Parse
                 * TryParse
                 * TryParse
                 * GetTypeCode
                 * GetType
                 */
                Console.WriteLine(methodInfo.Name);
            }
        }

        /**
         * default() 操作符: 时间51:30
         * 用于获取数据类型的默认值
         */
        public static void default_exam() {
            var defaultInt = default(int);
            const int defaultInt2 = default(int);
            const int defaultInt3 = default;
            Console.WriteLine("{0}, {1}, {2}", defaultInt, defaultInt2, defaultInt3); //0, 0, 0

            var person = default(Person);
            Console.WriteLine("person == null: {0}", person == null); //True

            var enumL = default(EnumLevel);
            Console.WriteLine(enumL);   //Low
        }
    }
}